package com.hamizhong.iflowers.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IGoodsDao;
import com.hamizhong.iflowers.db.dao.hibernate.IGoodsProductMapDao;
import com.hamizhong.iflowers.db.dao.hibernate.IProductDao;
import com.hamizhong.iflowers.db.entity.Goods;
import com.hamizhong.iflowers.db.entity.GoodsProductMap;
import com.hamizhong.iflowers.db.entity.Product;
import com.hamizhong.iflowers.service.IGoodsProductMapService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.GoodsProductMapVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class GoodsProductMapServiceImpl extends MyBaseService implements IGoodsProductMapService {
	private IGoodsDao iGoodsDao = null;
	private IProductDao iProductDao = null;
	private IGoodsProductMapDao iGoodsProductMapDao = null;
	
	public IGoodsDao getiGoodsDao() {
		return iGoodsDao;
	}
	public void setiGoodsDao(IGoodsDao iGoodsDao) {
		this.iGoodsDao = iGoodsDao;
	}
	public IProductDao getiProductDao() {
		return iProductDao;
	}
	public void setiProductDao(IProductDao iProductDao) {
		this.iProductDao = iProductDao;
	}
	public IGoodsProductMapDao getiGoodsProductMapDao() {
		return iGoodsProductMapDao;
	}
	public void setiGoodsProductMapDao(IGoodsProductMapDao iGoodsProductMapDao) {
		this.iGoodsProductMapDao = iGoodsProductMapDao;
	}
	public IOPResultDto<IObjectVO> addGoodsProductMaps(List<GoodsProductMapVO> goodsProductMapVOList) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			for (GoodsProductMapVO goodsProductMapVO : goodsProductMapVOList) {
				GoodsProductMap goodsProductMap = new GoodsProductMap();
				BeanUtils.copyProperties(goodsProductMapVO, goodsProductMap);
				Goods goods = iGoodsDao.findById(goodsProductMapVO.getGoodsVO().getGoodsId());
				goodsProductMap.setGoods(goods);
				Product product = iProductDao.findById(goodsProductMapVO.getProductVO().getProductId());
				goodsProductMap.setProduct(product);
				iGoodsProductMapDao.add(goodsProductMap);
			}
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
